<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>vue路由hash模式实现原理</h1>
    <button id="btn">切换hash值</button>
</body>
</html>
<script>
    const btn = document.getElementById('btn')

    // 1、监听hash值变化(浏览器前进后退都会触发) 浏览器不刷新 页面刷新获取最新
    window.onhashchange = (e => {
        console.log('老url',e.oldURL);
        console.log('新url',e.newURL);
        console.log('hash',location.hash);
    })
    // 2、点击事件切换hash值
    btn.addEventListener('click',()=>{
        location.href = '#/user'
    })
    // 3、页面初始化加载时 获取hash
    window.addEventListener('DOMContentLoaded',()=>{
        console.log('hash',location.hash);
    })
</script>